package math
{
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;

	public class SpiralCurve extends Sprite
	{
		private var rad:Number = 0;
		private var numerator:int = 3;
		private var r:Number = 0;
		private var n:int = 0;
		private var j:int = 0;
		
		public function SpiralCurve(r:Number, n:int, stage:Stage)
		{
			this.graphics.moveTo(0,0);
			this.creatSpiralCurve(r, n);
			this.x = stage.stageWidth / 2;
			this.y = stage.stageHeight / 2;
			stage.addEventListener(Event.ENTER_FRAME, run);
			stage.addEventListener(MouseEvent.CLICK, clickHandler);
		}
		
		
		
		/** 创建2D的螺旋线*/
		public function creatSpiralCurve(r:Number,n:int):void
		{			
			this.rad = 2*Math.PI / this.numerator;
			this.r = r;
			this.n = n;
		}
		
		protected function run(event:Event):void
		{
			this.j++;
			if(j < 360 * n){
				var len:Number=r*j*rad;
				var point:Point=Point.polar(len,j*rad);
				drawPoint(point);
			}
		}
		
		protected function clickHandler(evt:Event):void
		{
			this.j = 0;
			this.numerator ++;
			this.rad = 2*Math.PI / this.numerator;
			this.graphics.clear();
		}
		
		/** 描点*/
		private function drawPoint(point:Point):void
		{
			this.graphics.lineStyle(1);
			this.graphics.lineTo(point.x,point.y);
		}
	}
}